package com.starbaba.stat.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.StatLuckyTurnTableModel;
import com.starbaba.stat.utils.Constant;

@SuppressWarnings("rawtypes")
@Component
public class StatLuckyTurnTableService {

	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);
	
	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String category = (String) paramsMap.get("category");
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		StringBuilder sql_list = new StringBuilder("select * from stat_lucky_turn_table where category = ? ");
		if (StringUtils.isNotEmpty(startTime)) {
			sql_list.append("and date >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)) {
			sql_list.append("and date <= '" + endTime + "' ");
		}
		sql_list.append("order by date desc ");
		
		String sql_total = "select count(1) from (" + sql_list + ") z " ;
		if (start != null && length != null) {
			sql_list.append("limit ").append(start).append(", ").append(length);
		}
		
		int count = dao.getInt(sql_total, category);
		List<StatLuckyTurnTableModel> list = dao.list(sql_list.toString(), StatLuckyTurnTableModel.class, category);
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
	}
	
}
